import ReactDOM from "react-dom";
import NcVerify from "./NcVerify";

class NcVerifyHandler {
  el = null;

  show(options = {}) {
    this.checkAndNewMountedDom();
    !!this.el &&
      ReactDOM.render(
        <NcVerify
          showTips={options.showTips}
          expirationTime={options.expirationTime}
          close={() => this.hide()}
          confirm={async (e) => {
            try {
              !!options.confirm && (await options.confirm(e));
            } catch (error) {
              return Promise.reject();
            }
          }}
        />,
        this.el
      );
  }

  hide() {
    const dom = document.getElementById("numcode-verify");
    dom?.remove();
  }

  checkAndNewMountedDom() {
    let dom = document.getElementById("numcode-verify");
    if (!dom) {
      dom = document.createElement("div");
      dom.id = "numcode-verify";
      document.body.append(dom);
    }
    this.el = dom;
  }
}

export default new NcVerifyHandler();
